package com.whc.common.exception.handle;

import com.whc.common.exception.MessageException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wang-hai-cheng
 * @since 2021/12/11 22:27
 */
@RestControllerAdvice
public class ExceptionHandle {
    /**
     * 全局异常捕捉处理
     *
     * @param ex 异常实例
     * @return 错误内容
     */
    @ExceptionHandler(value = MessageException.class)
    public ResponseEntity<?> messageException(MessageException ex) {
        return ResponseEntity.ok(ex.getMessage());
    }

    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<?> runtimeException(RuntimeException ex) {
        ex.printStackTrace();
        return ResponseEntity.ok("服务器异常");
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<?> exception(Exception ex) {
        ex.printStackTrace();
        return ResponseEntity.ok("服务器异常");
    }
}